package com.bw.shopping.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.R;
import com.bw.shopping.adapter.AddressAdapter;
import com.bw.shopping.entity.DeletEntity;
import com.bw.shopping.entity.GetAddressEntity;
import com.bw.shopping.mvvm.PostSticky;
import com.bw.shopping.mvvm.model.AddressModel;
import com.bw.shopping.mvvm.view.fragment.CompileActivity;
import com.bw.shopping.mvvm.view.fragment.ShipAddressActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/29 0029 下午 6:41
 *
 */
class AddressViewModel extends BaseViewModel<AddressModel> implements OnItemChildClickListener{
    public BaseQuickAdapter adapter = new AddressAdapter();
    List<GetAddressEntity.DataBean> data;
    int user_id;
    @Override
    public void onCreate() {
        super.onCreate();
        requestAddress();
    }
    public void requestAddress(){

        user_id = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list().get(0).getUser_id();
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",user_id);
        m.requestgetAddress(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof GetAddressEntity){
            GetAddressEntity getAddressEntity = (GetAddressEntity) entity;
            data = getAddressEntity.getData();
            adapter.setNewInstance(data);
            adapter.setOnItemChildClickListener(this);

        }else if (entity instanceof DeletEntity){
            DeletEntity deletEntity = (DeletEntity) entity;
            if (deletEntity.getMessage().equals("刪除成功")) {
                ToastUtils.showLong("删除地址成功");
            }

        }
    }

    public void deletAddress(int position){
        HashMap<String,Object> map = new HashMap<>();
        map.put("id",data.get(position).getId());
        map.put("userId",user_id);
        m.requestdelet(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    public void finish(){
        finishPage();
    }
    //新建地址
    public void createAddress(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, ShipAddressActivity.class);
        startPage(map);

    }

    @Override
    public void onItemChildClick( BaseQuickAdapter adapter,  View view, int position) {
        int id = view.getId();
        if (id==R.id.mSetDefaultTv){

                    String ship_user_name = data.get(position).getShip_user_name();
                    String ship_address = data.get(position).getShip_address();
                    String ship_user_mobile = data.get(position).getShip_user_mobile();
                    int id1 = data.get(position).getId();
                    PostSticky postSticky = new PostSticky();
                    postSticky.myusername =ship_user_name;
                    postSticky.useraddress = ship_address;
                    postSticky.phone = ship_user_mobile;
                    postSticky.addressid = id1;
                    EventBus.getDefault().postSticky(postSticky);

        }else if (id==R.id.mDeleteTv){

                    deletAddress(position);
                    data.remove(position);
                    adapter.notifyItemRemoved(position);

        }else if (id == R.id.mEditTv){
                    Bundle bundle = new Bundle();
                    bundle.putInt("id",data.get(position).getId());
                    HashMap<String,Object> map = new HashMap<>();
                    map.put(UIField.PAGEBUNDLE,bundle);
                    map.put(UIField.PAGECLAZZ, CompileActivity.class);
                    startPage(map);
        }
    }
}
